<template>
  <y-dialog @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="addRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="分析结论" prop="analysis">
            <y-select
              v-model="form.analysis"
              placeholder="请选择分析结论"
              :options="oms_hawkeye_analysis"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="5"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="标签" prop="tags">
          <y-tag v-model="form.tags" :closable="true" />
        </el-form-item>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="Analysis">
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();

const emit = defineEmits(["submited", "close"]);

const { oms_hawkeye_analysis } = proxy.useDict("oms_hawkeye_analysis");

const data = reactive({
  form: {},
  rules: {
    analysis: [
      { required: true, message: "分析结论不能为空", trigger: "blur" },
    ],
    remark: [{ required: true, message: "备注信息不能为空", trigger: "blur" }],
  },
});

const { form, rules } = toRefs(data);

const props = defineProps({
  data: { type: Array, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 提交按钮事件 */
async function submit() {
  try {
    if (!proxy.$refs.addRef || !(await proxy.$refs.addRef.validate())) return;
    const userInfo = await useUserStore().getInfo();
    for (const val of props.data) {
      await props.updateMethod?.apply({
        id: val.id,
        status: "1",
        tags: form.value.tags || [],
        remark: form.value.remark,
        analyser: userInfo.data.user.id,
        analysis: form.value.analysis,
      });
    }
    proxy.$modal.msgSuccess("操作成功");
    submited();
  } catch (err) {
    proxy.$modal.msgError(`操作失败，失败原因请参考：${err}`);
  }
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
